package com.game.server.message.proto.execute;

import java.net.InetAddress;
import java.sql.Timestamp;
import java.util.HashSet;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.vo.UserVo;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.message.proto.in.AbstractZhanghaoLoginReq;
import com.game.server.message.proto.out.LoginRsp;
import com.game.server.message.proto.out.NoticeDifferentPlacesLoginRsp;
import com.game.server.message.proto.out.VersionNumberRsp;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.util.Util;
import lombok.extern.log4j.Log4j;


public class ZhanghaoLoginReq extends AbstractZhanghaoLoginReq {

    @Override
    public void Execute(ISession session) {
		log.info("登录");
		String password = Util.Md5(mima);
		LoginRsp rsp = new LoginRsp();
//		if ("0.0".equals(longitude) || "0.0".equals(latitude)|| "".equals(longitude) || "".equals(latitude) ) {
//			rsp.setIsOK((byte) 5);//微信登录没有GPS定位
//			session.send(rsp.Serialize());
//			return ;
//		}

		rsp.setPassword(mima);
		rsp.setUnionid(account);
	LogManager.getInstance().Log("ZhanghaoLoginReq:  account:"+account+"   mima:"+mima);
		String str = DataManager.getInstance().systemValue("versionNumber");

		Double d = Double.valueOf(str);
		Double newd = Double.valueOf(version);
		LogManager.getInstance().Log("登陆接口:  str:"+account+"   mima:"+mima);
		if(newd < d){
			VersionNumberRsp vrsp = new VersionNumberRsp();
			vrsp.setIsok(0);
			vrsp.setShare(DataManager.getInstance().systemValue("shareurl"));
			session.send(vrsp.Serialize());
			rsp.setIsOK((byte) 12);	//版本过低
			session.send(rsp.Serialize());
			return;
		}
		
		int userid = UserVo.CheckAccount(account);
		if (userid == -1) {
			rsp.setIsOK((byte) 10);//用户未注册
			session.send(rsp.Serialize());
			return;
		}
		int uid = UserVo.CheckPassword(account,password);

		//    	LogManager.getInstance().Log("account:"+account+"   password:"+password+"  mima:"+mima);


		LogicClient client = (LogicClient) LogicServer.getInstance().getClient(uid);
		if(uid > 0) {

				if(client!=null){

					LogManager.getInstance().Log("已经登陆了 或者微信登陆 ");
					if (client.getVo().getLimitipgps() == 0 && ("0.0".equals(longitude) || "0.0".equals(latitude) || "".equals(longitude) || "".equals(latitude)) ) {
						rsp.setIsOK((byte) 5);//微信登录没有GPS定位
						session.send(rsp.Serialize());
						return ;
					}
//					if(!client.isLost() && !client.getEquipmentNumber().equals(this.equipmentNumber)) {
//						rsp.setIsOK((byte) 13);//
//						session.send(rsp.Serialize());
//						return;
//					} 
					if(!client.getEquipmentNumber().equals(this.equipmentNumber)){
						NoticeDifferentPlacesLoginRsp ndprsp = new NoticeDifferentPlacesLoginRsp();
						client.Send(ndprsp);
						client.setEquipmentNumber(this.equipmentNumber);
					}
					client.setSession(session);
					rsp.setName(client.getVo().getNickName());
					rsp.setUid(client.getUserId());
					rsp.setHead(client.getVo().getHead());
					rsp.setIsOK((byte) 7);//断线重连
					if (client.getTable() != null) {
						rsp.setIsOK((byte) 8);//断线重连  有房间
					}
					
				}else{

					if (!DataManager.getInstance().isServerLoginBool()) {
						rsp.setIsOK((byte) 11);//禁止登录 维护中
						session.send(rsp.Serialize());
						return;						
					}
					int jinyong = UserVo.checkToken(account,"");
					//LogManager.getInstance().Log("维护中禁止登录jinyong："+String.valueOf(jinyong));
					if(jinyong==-1){
						rsp.setIsOK((byte) 9);//用户禁用
						session.send(rsp.Serialize());
						return;
					}
					client = new LogicClient(uid, session);
					if (client.getVo().getLimitipgps() == 0 ) {
						if (("0.0".equals(longitude) || "0.0".equals(latitude) || "".equals(longitude) || "".equals(latitude))) {
							rsp.setIsOK((byte) 5);//微信登录没有GPS定位
							session.send(rsp.Serialize());
							return ;
						} else {
							if (client.getVo().getCreatelongitude() == null || client.getVo().getCreatelatitude() == null || 
									"0.0".equals(client.getVo().getCreatelongitude()) || "0.0".equals(client.getVo().getCreatelatitude())) {
								client.getVo().setCreatelongitude(longitude);
								client.getVo().setCreatelatitude(latitude);
								client.getVo().updateUserCreateposition(longitude+"&"+latitude);
							}
							client.getVo().addListWarpAndWefts(longitude+"&"+latitude+"&"+System.currentTimeMillis());
						}
					}
//					if (client.getVo().getLimitipgps() == 0 && ("0.0".equals(longitude) || "0.0".equals(latitude) || "".equals(longitude) || "".equals(latitude)) ) {
//						rsp.setIsOK((byte) 5);//微信登录没有GPS定位
//						session.send(rsp.Serialize());
//						return ;
//					}

					if(LogicServer.getInstance().login(client))
					{

						rsp.setIsOK((byte) 0);
						rsp.setUid(client.getUserId());
						rsp.setHead(client.getVo().getHead());
						rsp.setName(client.getVo().getNickName());
						client.setEquipmentNumber(equipmentNumber);
						LogManager.getInstance().Log("客戶端id："+client.getUserId());
						 UserInfoData userInfoData = DataManager.getInstance().getMapUserData().get(client.getUserId());

						 if(userInfoData == null) {
							 userInfoData = new UserInfoData();
							 userInfoData.setAutograph(client.getVo().getAutograph());
							 userInfoData.setClubids(new HashSet<>());
							 userInfoData.setHead(client.getVo().getHead());
							 userInfoData.setId(client.getUserId());
							 LogManager.getInstance().Log("client.getVo().getLastTime()："+client.getVo().getLastTime());
							 userInfoData.setLogintime( new Timestamp(Util.dateToStamp(client.getVo().getLastTime())));
							 userInfoData.setName(client.getVo().getNickName());
							 userInfoData.setSex(client.getVo().getSex());
							 DataManager.getInstance().getMapUserData().put(client.getUserId(), userInfoData);
						 }

						 int notifCount = XyNotificationLogDao.findMyNotifCount(client.getUserId());
						 if (notifCount>0){
							 XYInformDataAlterRsp alterRsp = new XYInformDataAlterRsp();
							 alterRsp.setType(6);
							 alterRsp.setOperateclubid(client.getUserId());
							 alterRsp.setClubid(0);
							 alterRsp.setUnionid(0);
							 client.Send(alterRsp);
						 }

					}
					else
					{
						rsp.setIsOK((byte) 2);	//重复登录
					}
				}
				if(rsp.getIsOK() != 2)
				{
					client.getVo().setLongitude(longitude);
					client.getVo().setLatitude(latitude);
				}
				LogManager.getInstance().Log(client.getVo().getNickName()+"  登录成功!  longitude:"+longitude +"  latitude:"+latitude+ "   rsp"+rsp.getIsOK());

//			}else{
//				VersionNumberRsp vrsp = new VersionNumberRsp();
//				vrsp.setIsok(0);
//				vrsp.setShare(DataManager.getInstance().systemValue("shareurl"));
//				session.send(vrsp.Serialize());
//				rsp.setIsOK((byte) 8);	//版本过低
//			}



		}else {
			rsp.setIsOK((byte) 3);	//用户名密码错误
		}
		session.send(rsp.Serialize());

		
	}

}
